package net.zjitc.controller;


import net.zjitc.common.Msg;
import net.zjitc.common.ServiceResponse;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.ProductService;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/24 14:30
 */
@Controller
public class Usercontroller{
    @Autowired
    UserService userService;
    @Autowired
    ProductService productService;

    @RequestMapping("register")
    public String register(Model model, User user){
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password);
        user.setRole("普通用户");
        Date date = new Date();
        userService.register(user);
        return "/suyaoyajiu/login.jsp";
    }

    @RequestMapping("/login")
    public String login(Model model, User user, HttpServletRequest request){
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        User findedUser = userService.findByName(user.getUsername());
        if (findedUser==null){
            model.addAttribute("msg","用户名或密码不能为空");
            return "/suyaoyajiu/login.jsp";
        }
        if (password.equals(findedUser.getPassword())){
            HttpSession session = request.getSession();
            session.setAttribute("user",findedUser);
            return "/product/index.do";
        }
        model.addAttribute("msg","用户名或密码错误（密码）");
        return "/suyaoyajiu/login.jsp";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "/index.jsp";
    }
    @RequestMapping(value = "/checkEmail")
    public Msg checkEmail(User user){
        return userService.checkEmail(user);
    }
    @RequestMapping(value = "/checkUsername")
    public Msg checkUsername(User user){
        return userService.checkUsername(user);
    }



    @RequestMapping("/update")
    @ResponseBody
    public ServiceResponse update(@RequestBody User user, HttpServletRequest request,HttpSession session){
        return userService.update(user,request,session);
    }

    @RequestMapping("/tiaozhuan")
    public String tiaozhuan(HttpSession session){
        User user = (User) session.getAttribute("user");
        User user1 = userService.findById(user.getId());
        session.setAttribute("user",user1);
        return "/suyaoyajiu/personalcenter.jsp";
    }

}

